package com.liyujie.networkrequest.utils;

import android.text.TextUtils;
import android.view.Gravity;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.liyujie.networkrequest.BaseApplication;
import com.liyujie.networkrequest.R;
import com.liyujie.networkrequest.common.Constants;
import com.liyujie.networkrequest.remotedata.AppComponent;
import com.liyujie.networkrequest.remotedata.DaggerAppComponent;
import com.liyujie.networkrequest.remotedata.SpModule;
import com.liyujie.networkrequest.remotedata.systemapi.SystemApiServiceModule;
import com.liyujie.networkrequest.utils.serialport.OpenPortUtils;
import com.liyujie.networkrequest.utils.serialport.SwipeCard;
import com.mc.enjoysdk.McSecure;
import com.mc.enjoysdk.McTime;
import com.mc.enjoysdk.result.McResultBool;
import com.mc.enjoysdk.transform.McErrorCode;
import com.mc.enjoysdk.transform.McSecurePasswordState;

public class InitializeUtils {
    /**
     *  上下文
     */
    private static BaseApplication MBaseApplication;
    public static BaseApplication getMBaseApplication() {
        return MBaseApplication;
    }
    public static void setMBaseApplication(BaseApplication MBaseApplication) {
        InitializeUtils.MBaseApplication = MBaseApplication;
    }


    /**
     * Toast设置统一样式
     */
    private static ToastUtils defaultMaker;
    public static ToastUtils getDefaultMaker() {
        return defaultMaker;
    }
    public static void initDefaultMaker() {
        defaultMaker = ToastUtils.getDefaultMaker();
        defaultMaker.setBgColor(ColorUtils.getColor(R.color.color_1E90FF));
        defaultMaker.setGravity(Gravity.CENTER,0,0);
        defaultMaker.setTextColor(ColorUtils.getColor(R.color.color_FFFFFF));
        defaultMaker.setTextSize(26);
    }


    /**
     * 初始化v
     */
    private static AppComponent mAppComponent;
    public static AppComponent getAppComponent() {
        return mAppComponent;
    }
    public static void setAppComponent(BaseApplication mBaseApplication) {
        mAppComponent = DaggerAppComponent.builder().systemApiServiceModule(new SystemApiServiceModule()).spModule(new SpModule(mBaseApplication)).build();
    }


    /**
     * 日志相关
     */
    public static void initializeLogUtils(){
        LogUtils.Config config = LogUtils.getConfig();
        //设置 log 总开关
        config.setLogSwitch(true);
        //设置 log 文件开关
        config.setLog2FileSwitch(true);
        //设置 log 可保留天数
        config.setSaveDays(5);
        // 设置 log 文件存储目录
        config.setDir(Constants.LogPath);
    }


    /**
     * 串口初始化
     */
    public static void initGorge(){
        //串口初始化
        OpenPortUtils.InitializeTheOpenSerialPort();
        //刷卡串口
        SwipeCard.initializeCreditCardSerialPort();
    }


    /**
     * NTP服务器
     * @param mBaseApplication
     */
    public static void initNTP(BaseApplication mBaseApplication){
        McSecure mcSecure = McSecure.getInstance(mBaseApplication);
        int ret = mcSecure.getSecurePasswdStatus();
        System.out.println("securePasswdStatus: " + ret);
        if (ret == McSecurePasswordState.MC_SECURE_PASSWD_EMPTY) {
            ret = mcSecure.setSecurePasswd(null, "Abc12345");
            System.out.println("设置安全密码结果： " + ret);
        }
        boolean rest = false;
        do {
            if (mcSecure.checkSafeProgramOfSelf() == McResultBool.TRUE) {
                System.out.println("安全应用校验通过。");
                rest = true;
                break;
            } else {
                System.out.println("安全应用校验未通过。");
            }
            ret = mcSecure.registSafeProgram("Abc12345");
            if (ret == McErrorCode.ENJOY_COMMON_SUCCESSFUL) {
                System.out.println("安全应用注册通过。");
                rest = true;
            } else {
                System.out.println("安全应用注册未通过。 注册结果： " + ret);
            }
        } while (false);
        if (rest) {
            String ntps = McTime.getInstance(mBaseApplication).getNtpServerAddressInUse();
            System.out.println("NTP服务器: " + ntps);
            if (ntps != null && !ntps.equals("ntp.aliyun.com")) {
                int res = McTime.getInstance(mBaseApplication).setNtpServerAddress("ntp.aliyun.com", 5);
                System.out.println("设置NTP服务器为: ntp.aliyun.com " + res);
            }
        }

    }

}
